// 获取哈希类型的灰度配置
function getCompanyConfig() {
  const result = {};
  const configList =
    document.getElementById('parsed_content').value.split('\n') || [];
  configList.forEach(configStr => {
    const [key, value] = configStr.split(':');
    // 字符类型数组 "['fktest289']" 转数组 ['fktest289']
    result[key] = eval(value.match(/\[[^\[\]]*\]/g)[0]);
  });
  return result;
}

function checkCompany() {
  const companyStr = prompt(
    '请输入需校验灰度企业字符串（以逗号、分号或空格分隔）'
  );
  let checkPass = true;

  if (!companyStr) return;
  // 去除引号，再以逗号或分号或空格分割
  const companyList =
    companyStr.replace(/["'“”‘’]+/g, '').match(/[^;；,， ]+/g) || [];
  const configMap = getCompanyConfig();
  const repetitionCompanyMap = {};

  Object.keys(configMap).forEach(key => {
    const currentExitCompanyList = configMap[key];

    if (!currentExitCompanyList.length) return;
    const repetitionCompanyList = [];

    companyList.forEach(company => {
      if (currentExitCompanyList.indexOf(company) > -1) {
        repetitionCompanyList.push(company);
        repetitionCompanyMap[company] = true;
      }
    });

    if (repetitionCompanyList.length > 0) {
      checkPass = false;
      console.error(
        `企业已经存在！(${key}): `,
        repetitionCompanyList.join(', ')
      );
    }
  });
  const notRepeatCompanyList = companyList.filter(
    company => !repetitionCompanyMap[company]
  );
  console.log('未重复企业：', notRepeatCompanyList.join(','));
  checkPass && console.log('没有已存在企业！');
}

checkCompany();
